package com.huiminpay.merchant.controller;

import com.aliyun.oss.OSS;
import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.util.PhoneUtil;
import com.huiminpay.merchant.api.MerchantService;
import com.huiminpay.merchant.common.util.SecurityUtil;
import com.huiminpay.merchant.convert.MerchantDetailConvert;
import com.huiminpay.merchant.convert.MerchantRegisterConvert;
import com.huiminpay.merchant.dto.MerchantDTO;
import com.huiminpay.merchant.service.FileService;
import com.huiminpay.merchant.service.SmsService;
import com.huiminpay.merchant.vo.MerchantDetailVO;
import com.huiminpay.merchant.vo.MerchantRegisterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Api(value = "商户平台", tags = "商户", description = "商户平台应用")
@RestController
public class MerchantController {

    @Reference //dubbo注解  注入远程服务
    MerchantService merchantService;
    @Autowired //注入本地接口
    SmsService smsService;
    @Autowired
    OSS oss;
    @Autowired
    FileService fileService;


    @ApiOperation("根据id查询商户")
    @ApiImplicitParam(name = "id", value = "商户id", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/merchants/{id}")
    public MerchantDTO queryMerchantById(@PathVariable("id") Long merchantId) {
        return merchantService.queryMerchantById(merchantId);
    }

    @ApiOperation("获取登录用户的商户信息")
    @GetMapping(value="/my/merchants")
    public MerchantDTO getMyMerchantInfo(){
        Long merchantId = SecurityUtil.getMerchantId();
        MerchantDTO merchantDTO = merchantService.queryMerchantById(merchantId);
        return merchantDTO;
    }

    //发送手机验证码
    @ApiOperation("发送验证码")
    @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query")
    @GetMapping("/sms")
    public String getSms(String phone) {
        //调用验证码服务

        return smsService.sendSms(phone);
    }

    @ApiOperation("注册商户")
    @ApiImplicitParam(name = "merchantRegister", value = "注册信息", required = true, dataType =
            "MerchantRegisterVo", paramType = "body")
    @PostMapping("/merchants/register")
    public MerchantRegisterVo registerMerchant(@RequestBody MerchantRegisterVo registerVo) {

        //校验
        if (registerVo == null) {
            throw new BusinessException(CommonErrorCode.E_100108);
        }

        // 手机号非空校验
        if (registerVo.getMobile() == null) {
            throw new BusinessException(CommonErrorCode.E_100112);
        }

        //手机号格式校验
        if (!PhoneUtil.isMatches(registerVo.getMobile())) {
            throw new BusinessException((CommonErrorCode.E_100109));
        }

        //用户名
        if (registerVo.getUsername() == null) {
            throw new BusinessException(CommonErrorCode.E_100110);
        }
        //密码为空
        if (registerVo.getPassword() == null) {
            throw new BusinessException(CommonErrorCode.E_100111);
        }

        //校验验证码
        if (StringUtils.isEmpty(registerVo.getVerifiyCode() == null)) {
            throw new BusinessException(CommonErrorCode.E_100103);
        }

        //校验验证码
        smsService.checkVerifyCode(registerVo.getVerifiykey(), registerVo.getVerifiyCode());
        //注册商户
//        MerchantDTO merchantDTO = new MerchantDTO();
//        merchantDTO.setUsername(registerVo.getUsername());
//        merchantDTO.setPassword(registerVo.getPassword());
//        merchantDTO.setMobile(registerVo.getMobile());

        //voToDto
        MerchantDTO merchantDTO = MerchantRegisterConvert.INSTANCE.voToDto(registerVo);

        merchantService.createMerchant(merchantDTO);

        return registerVo;
    }


    //上传证件照
    @ApiOperation("上传证件照")
    @PostMapping("/upload")
    @ApiImplicitParam(name = "file", value = "文件", dataType = "MultipartFile", paramType = "body")
    public String uploaf(MultipartFile file) {
        return fileService.uploadFile(file);
    }

    //商户资质申请
    @ApiOperation("资质申请")
    @PostMapping("/my/merchants/save")
    @ApiImplicitParam(name = "merchantInfo", value = "商户认证资料", required = true, dataType = "MerchantDetailVO", paramType = "body")
    public void saveMerchant(@RequestBody MerchantDetailVO merchantInfo) {

        //从 token中解析商户id
        Long merchantId = SecurityUtil.getMerchantId();

        MerchantDTO merchantDTO = MerchantDetailConvert.INSTANCE.voToDto(merchantInfo);

        merchantService.applyMerchant(merchantId,merchantDTO);
    }

//    @ApiOperation("测试")
//    @GetMapping("/hello")
//    public String hello(){
//        return "hello";
//    }
//
//    @ApiOperation("测试2")
//    @ApiImplicitParam(name = "name",value = "姓名",dataType = "String",paramType = "query")
//    @PostMapping("/hi")
//    public String hi(String name){
//        return "hi " + name;
//    }


}
